package com.miyake.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.miyake.client.DoctorsUserService;
import com.miyake.client.LoginInfo;
import com.miyake.client.community.CommunityProperty;
import com.miyake.client.datamanage.plugin.PluginProperty;
import com.miyake.client.profile.UserProperty;
import com.miyake.client.tools.ImageListElement;
import com.miyake.server.user.UserManager;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DoctorsUserServiceImpl extends RemoteServiceServlet implements DoctorsUserService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6256048146302823012L;

	private UserManager userManager = UserManager.getInstance();
	
	@Override
	public LoginInfo login(String requestUri) {
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
	    LoginInfo loginInfo = new LoginInfo();
	    
	    if(user != null && userManager.isValidUser(user.getEmail())){
			loginInfo.setLoggedIn(true);
			loginInfo.setEmail(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
	    }
	    else{
	      loginInfo.setLoggedIn(false);
	      loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
	    }
	    
	    return loginInfo;
	}

	@Override
	public List<CommunityProperty> getAllCommunityList(Integer type) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<CommunityProperty> ret = new ArrayList<CommunityProperty>();
		try {
			String query = "select from " + DsCommunity.class.getName();
			query += " where type == " + type.toString();
			List<DsCommunity> list = (List<DsCommunity>) pm.newQuery(query).execute();
			for (DsCommunity d : list) {
				ret.add(d.toRemoteObject());
			}
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			pm.close();
		}
		return ret;
	}

	@Override
	public String createCommunity(String name, Integer type) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String ret = null;
		try {
			DsCommunity com = new DsCommunity();
			com.setName(name);
			com.setType(type);
			pm.makePersistent(com);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			pm.close();
		}
		return ret;
	}

	@Override
	public void deleteCommunity(String key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String ret = null;
		try {
			DsCommunity com = pm.getObjectById(DsCommunity.class, key);
			pm.deletePersistent(com);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			pm.close();
		}
	}

	@Override
	public void addUser(String email) {
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
	    
	    if (user.getEmail().equals("miyakemusic@gmail.com")) {
//	    	if (userManager.isValidUser(email) == false) {
	    		userManager.createUser(email);
//	    	}
	    }
	}

	@Override
	public Boolean isAdministrator() {
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
		if (user.getEmail().equals("miyakemusic@gmail.com")) {
			return true;
		}
		return false;
	}

	@Override
	public void deleteUser(String key) {
		userManager.deleteUser(key);
	}

	@Override
	public UserProperty getUser(String email) {
		return userManager.getUser(email).toRemoteObject();
	}

	@Override
	public UserProperty getMyProperty() {
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
		return userManager.getUserByEmail(user.getEmail()).toRemoteObject();
	}

	@Override
	public void updateMyProperty(Integer row, String value) {
	    userManager.updateUserElement(getMyKey(), row, value);
	}

	@Override
	public List<ImageListElement> getFriendList(String key) {
		List<ImageListElement> ret = new ArrayList<ImageListElement>();
		List<DsUser> users = userManager.getFriendList(key);
		for (DsUser user : users) {
			ret.add(user.toImageListElement());
		}
		return ret;
	}

	@Override
	public List<ImageListElement> getAllUsers() {
		return userManager.getAllUsers();
	}

	@Override
	public void addFriend(String key) {
		userManager.addFriend(getMyKey(), key);
	}

	@Override
	public void removeFriend(String key) {
		userManager.removeFriend(getMyKey(), key);
	}

	@Override
	public List<ImageListElement> getCommunityList(String key, Integer type) {
		List<ImageListElement> ret = new ArrayList<ImageListElement>();
		List<DsCommunity> comms = userManager.getCommunityList(key, type);
		for (DsCommunity com : comms) {
			ret.add(com.toCommunityProperty());
		}
		return ret;
	}

	@Override
	public void removeFaceImage(String key) {
		userManager.removeFaceImage(key);
	}

	@Override
	public List<ImageListElement> getFaceImages(String key) {
		DsUser user = userManager.getUser(key);
		return user.toImageList();
	}

	@Override
	public void setFaceImage(String key, Integer number) {
		userManager.setFaceImage(key, number);
	}

	@Override
	public void addAnalysisPlugin(String key) {
		userManager.addAnalysisPlugin(getMyKey(), key);
	}

	@Override
	public void removeAnalysisPlugin(String key) {
		userManager.removeAnalysisPlugin(getMyKey(), key);
	}

	@Override
	public List<PluginProperty> getAnalysisPluginList() {
		return userManager.getAnalysisPluginList(getMyKey());
	}

	@Override
	public void addFilePlugin(String key) {
		userManager.addFilePlugin(getMyKey(), key);
	}

	@Override
	public List<PluginProperty> getFilePluginList() {
		return userManager.getFilePluginList(getMyKey());
	}
	
	private String getMyKey() {
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
		return userManager.getKeyFromEmail(user.getEmail());
	}

	@Override
	public void removeFilePlugin(String key) {
		userManager.removeFilePlugin(getMyKey(), key);
	}

	@Override
	public void addChartPlugin(String key) {
		userManager.addChartPlugin(getMyKey(), key);
	}

	@Override
	public void removeChartPlugin(String key) {
		userManager.removeChartPlugin(getMyKey(), key);
	}

	@Override
	public List<PluginProperty> getChartPluginList() {
		return userManager.getChartPluginList(getMyKey());
	}
}

